import {getLocalStorageJson} from "@/util/utilString";
import {addMark, add, setHistory} from "@/music";

export default {
    // 开启命名空间
    namespaced: true,
    getters: {
        getMusicList(state) {
            state.musicList.length = 0;
            let historyList = getLocalStorageJson("historyList");
            addMark(historyList);
            state.musicList.push(...historyList);
            return state.musicList;
        }
    },
    actions: {},
    mutations: {
        EMPTY(state) {
            state.musicList.length = 0;
        },
        ADD_MUSIC_HISTORY(state, music) {
            setHistory(music, true);
            state.musicList.length = 0;
            state.musicList = state.musicList.filter((song) => {
                // 如果记录重复将去重
                return state.musicList.id !== song.id;
            })
            // 历史记录不能超过100条
            if (state.musicList.length >= 100) {
                state.musicList.length = 99;
            }
            state.musicList.unshift(music);
        },
        ADD_MUSIC_LIST_HISTORY(state, musicList) {
            state.musicList.unshift(...musicList);
        },
    },
    state() {
        return {
            musicList: []
        }
    },
}
